import Gun from "gun/gun";
import { User as GunUser } from "gun/sea";

const gun = Gun();
const user = gun.user().recall({ sessionStorage: false });

const promisify = (obj, method, args, success = null) => {
  function handler(resolve, reject) {
    obj[method](...args, r => {
      if (r.err) {
        reject(new Error(r.err));
        return;
      }
      resolve(success);
    });
  }

  return new Promise(handler.bind(this));
};

class User {
  create(alias, password) {
    return promisify(user, "create", [alias, password], user.is);
  }

  auth(alias, password) {
    return promisify(user, "auth", [alias, password], user.is);
  }
}

export { User };
